#include "param.h"
#include "types.h"
#include "stat.h"
#include "user.h"
#include "mmu.h"
#include "fs.h"
#include "fcntl.h"
#include "syscall.h"
#include "traps.h"
#include "memlayout.h"


void testdump()
{
  int *s;
  //int *i_ptr;
  
  //long test_malloc_size;

  //uint *pde, *pgdir;

  //char s[20];
  //printf(1,"[ls] malloc char\n");
  s = (int*) sbrk (1024);
  s[2] = 998;

  printf(1,"%x  %x  %x  s2 = %x\n",&s, &s[0], &s[1], &s[2]);
//  sbrk2 = (char*) malloc (1024 * 2);
  //sbrk2[1] = 2;
  //free(sbrk2);
  //printf(1,"[ls] free char\n");

  //sleep(500);   // sleep 10 sec

  //printf(1,"[ls] malloc int\n");
  //i_ptr = (int*) sbrk (200 * 4096);
  //i_ptr[0] = 10;
  //i_ptr[1] = 20;

  // test_malloc_size = 2048; // start at 2000 byte
  // while(1)
  // {
  //   while(1) 
  //   {
  //     sbrk2 = (char*) malloc (test_malloc_size);
  //     if(sbrk2 == 0) break;
  //     //test_malloc_size ++;
  //   }
  //   break;
  //   if(test_malloc_size == 1) break;
  //   test_malloc_size /= 2;
  // }

  // printf(1, "max malloc size = [%d]\n", test_malloc_size);
  //printf(1,"[ls] &i_ptr=%p\ti_ptr=%p\t*i_ptr=%p\n",&i_ptr, i_ptr, *i_ptr);
  //printf(1,"[ls] &i_ptr=%p\ti_ptr=%p\t*i_ptr=%p\n",&(i_ptr)+1, (i_ptr+1), *(i_ptr+1));
  //free(i_ptr);
  //printf(1,"[ls] free int\n");
  //getpid();
  //pgdir = (uint*) get_pgdir(getpid());
//  pde = &pgdir[PDX(i_ptr)];

  //printf(1,"[testdump]\tpgdir=%x %x\tpde=%x %x\n", pgdir, *pgdir,pde, *pde);
  //free(sbrk2);
  //printf(1,"%s\n", "run testdump");
  //exec("testdump", 0);
  //sleep(1000);   // sleep 10 sec
}

char *echoargv[] = { "echo", "ALL", "TESTS", "PASSED", 0 };
int stdout = 1;

void
exectest(void)
{
  printf(stdout, "exec test\n");
  if(exec("echo", echoargv) < 0){
    printf(stdout, "exec echo failed\n");
    exit();
  }
}

int
main(int argc, char *argv[])
{
  //int i;
  printf(1,"[testdump]\t--- start [pid=%d]---\n",getpid());
  testdump();
  //exectest();
  sleep(2000);   // sleep 10 sec
  printf(1,"[testdump]\t--- exit ---\n");
  exit();
  //return 0;
}
